/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package analisadorlexico;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 
 * @author ufabc
 */
public class Scanner {

	String entrada;
	BufferedReader datainput;
	int pos = 0;
	String listaReservada[] = { "se" };

	public Scanner() throws FileNotFoundException {
		datainput = new BufferedReader(new FileReader("teste.txt"));
	}

	public boolean isOperator(char c) {
		if (c == '<' || c == '>' || c == '=' || c == '!') {
			return true;
		} else {
			return false;
		}
	}

	public boolean isAnchor(char c) {
		if (c == ' ' || c == '\n' || c == '\t' || c == '\r') {
			return true;
		} else {
			return false;
		}
	}

	public boolean isReservedWord(String Symbol) {
		for (int i = 0; i < listaReservada.length; i++) {
			if (listaReservada[i].equalsIgnoreCase(Symbol))
				return true;
		}
		return false;
	}

	public Token nextToken() throws IOException {

		Token tk = null;
		String Symbol = "";
		char c = (char) datainput.read();
		datainput.mark(0);
		pos++;

		while (isAnchor(c)) {
			c = (char) datainput.read();
			pos++;
		}

		
		// identificando ID e palavra reservada
		if ((Character.isLowerCase(c) || Character.isUpperCase(c))) {
			Symbol = String.format("%s%s", Symbol, c);
			c = (char) datainput.read();
			
			while (Character.isLowerCase(c) || Character.isUpperCase(c)
					|| Character.isDigit(c)) {
				if (!isAnchor(c)) {
					Symbol = String.format("%s%s", Symbol, c);
				}
				c = (char) datainput.read();
				
				datainput.mark(pos);
				pos++;
			}

			if (isAnchor(c) || isOperator(c)) {

				if (isReservedWord(Symbol)) {
					tk = new Token(2, Symbol); // 2 - palavra
				} else {
					tk = new Token(1, Symbol);// 1 - ID
				}

				datainput.reset();
				pos--;
			}

			// identificando numeros
		} else if (Character.isDigit(c)) {
			Symbol = String.format("%s%s", Symbol, c);
			c = (char) datainput.read();
			
			while (Character.isDigit(c)) {
				
				// System.out.println(c);				
				Symbol = String.format("%s%s", Symbol, c);
				datainput.mark(pos);
				pos++;
				c = (char) datainput.read();
				
			}

			if (isAnchor(c) || isOperator(c)) {
				tk = new Token(3, Symbol); // 3 - numeros
				datainput.reset();
				pos--;

				// caso eu pare de identificar numeros e nao identifique
				// nem um limitador nem um operador
			} else {
				c = (char) datainput.read();
				
				while (!isAnchor(c) && !isOperator(c)) {
					Symbol = String.format("%s%s", Symbol, c);
					datainput.mark(pos);
					pos++;
					c = (char) datainput.read();
					
				}

				if (isAnchor(c) || isOperator(c)) {
					tk = new Token(0, Symbol); // 0 - erro
					datainput.reset();
					pos--;
				}
			}
		} else if (c == '\"') {
			Symbol = String.format("%s%s", Symbol, c);			
			c = (char) datainput.read();
			
			while (c != '\"' && c != '\n') {
				Symbol = String.format("%s%s", Symbol, c);
				c = (char) datainput.read();
				datainput.mark(pos);
				pos++;
				
			}

			if (c == '\"') {
				Symbol = String.format("%s%s", Symbol, c);					
				tk = new Token(4, Symbol); // 4 - texto
				datainput.reset();
				pos--;

				// caso encontre \n - Erro
			} else {
				tk = new Token(0, Symbol); // 0 - erro
				datainput.reset();
				pos--;
			}
		} else if (c == '>') {

			Symbol = String.format("%s%s", Symbol, c);
			c = (char) datainput.read();
			
			if (Character.isLowerCase(c) || Character.isUpperCase(c)
					|| Character.isDigit(c) || isAnchor(c)) {
				tk = new Token(5, Symbol); // 5 - operador
				datainput.reset();
				pos--;
			} else {
				if (c == '=') {

					Symbol = String.format("%s%s", Symbol, c);
					c = (char) datainput.read();
					
					if (Character.isLowerCase(c) || Character.isUpperCase(c)
							|| Character.isDigit(c) || c == ' ' || isAnchor(c)) {
						tk = new Token(5, Symbol); // 5 -
						// operador
						datainput.reset();
						pos--;
					}
				}
			}
		} else if (c == '<') {
			
			Symbol = String.format("%s%s", Symbol, c);
			c = (char) datainput.read();			
			
			
			if (Character.isLowerCase(c) || Character.isUpperCase(c)
					|| Character.isDigit(c) || isAnchor(c)) {
				tk = new Token(5, Symbol); // 5 - operador
				datainput.reset();
				pos--;
			} else {
				if (c == '=') {
					
					Symbol = String.format("%s%s", Symbol, c);
					c = (char) datainput.read();
					
					if (Character.isLowerCase(c) || Character.isUpperCase(c)
							|| Character.isDigit(c) || c == ' ' || isAnchor(c)) {
						tk = new Token(5, Symbol); // 5 -
						datainput.reset();
						pos--;
					}
				}
			}
		} else if (c == '!') {
			Symbol = String.format("%s%s", Symbol, c);
			c = (char) datainput.read();
			
			if (Character.isLowerCase(c) || Character.isUpperCase(c)
					|| Character.isDigit(c) || isAnchor(c)) {
				tk = new Token(5, Symbol); // 5 - operador
				datainput.reset();
				pos--;
			}
		} else if (c == '=') {
			
			Symbol = String.format("%s%s", Symbol, c);
			c = (char) datainput.read();
			
			if (Character.isLowerCase(c) || Character.isUpperCase(c)
					|| Character.isDigit(c) || isAnchor(c)) {
				tk = new Token(5, Symbol); // 5 - operador
				datainput.reset();
				pos--;
			} else {
				if (c == '=') {

					Symbol = String.format("%s%s", Symbol, c);
					c = (char) datainput.read();
					if (Character.isLowerCase(c) || Character.isUpperCase(c)
							|| Character.isDigit(c) || c == ' ' || isAnchor(c)) {
						tk = new Token(5, Symbol); // 5 - operador
						// operador
						datainput.reset();
						pos--;
					}
				}
			}
		}
		// datainput.reset();
		// char d = (char) datainput.read();
		// System.out.printf("Token reconhecido id: %d Symbol: %s\n",
		// tk.tokenId,tk.tokenSymbol);
		// System.out.printf("-%s-\n",d);
		return tk;
	}

}
